import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jd/tools/utils/library.dart';
import 'package:get/route_manager.dart';

/*
 * Kit 
 * 封装｜优化 Flutter、及第三方组件
 */
class Kit {
  // 简单的 Toast
  static showToast({
    required String text,
    Alignment? align,
    double radius = 5,
  }) {
    BotToast.showText(
      duration: Duration(milliseconds: 2500),
      text: text.toString(),
      textStyle: TextStyle(
        fontSize: 13,
        color: Colors.white,
      ),
      clickClose: true,
      contentColor: Color.fromRGBO(0, 0, 0, 0.7),
      contentPadding: EdgeInsets.fromLTRB(15, 11, 15, 12),
      borderRadius: BorderRadius.all(Radius.circular(radius)),
      align: align ?? Alignment(0, 0),
    );
  }

  // 显示加载动画
  static showLoading() {
    BotToast.showLoading();
  }

  // 隐藏加载动画
  static hideLoading() {
    BotToast.closeAllLoading();
  }

  // iconFont 组件
  static Icon iconFont(
    int icon, [
    Color color = Colors.black45,
    double size = 15,
  ]) {
    return Icon(
      IconData(icon, fontFamily: 'iconfont'),
      color: color,
      size: size,
    );
  }

  // 弹窗
  static Future openAlert(
    String content,
    BuildContext context, {
    required String title,
  }) async {
    var action = await showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: Text(content, style: TextStyle(fontSize: 14)),
          actions: <Widget>[
            // ignore: deprecated_member_use
            FlatButton(
              child: Text(
                '取消',
                style: TextStyle(color: Utils.hexToColor('#333333')),
              ),
              onPressed: () {
                Get.back(result: '取消');
              },
            ),
            // ignore: deprecated_member_use
            FlatButton(
              child: Text(
                '确定',
                style: TextStyle(color: Utils.hexToColor('#297CFF')),
              ),
              onPressed: () {
                Get.back(result: '确定');
              },
            ),
          ],
        );
      },
    );

    if (action == '确定') {
      return true;
    } else if (action == '取消') {
      return false;
    } else {
      return false;
    }
  }
}
